package java8.deom4;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.ToIntFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.Assert;
import org.junit.Test;

public class Tests2  {




	
	
	@Test
	public void test1() {		
		 Stream<String> s=Stream.of("a","b","c");
		 List<String>  strList=s.collect(Collectors.toList());		 
		 Assert.assertEquals(Arrays.asList("a", "b", "c"), strList);
		
	}
	
	/**
	 * 使用 for循环将字符串 转换为大写
	 */
	@Test
	public void test2() {
		Function<String, String> mapper = str -> str.toLowerCase();
		List<String> collected =Stream.of("a", "b", "hello").map(mapper).collect(Collectors.toList());		
		collected.stream().forEach(item -> System.out.println(item));		
	}
		
	@Test
	public void test3() {
		Function<String, Integer> mapper = str -> Integer.parseInt(str);
		List<Integer> collected =Stream.of("1", "2", "3").map(mapper).collect(Collectors.toList());		
		collected.stream().forEach(item -> System.out.println(item));		
	}
	
	@Test
	public void test4() {
		Predicate<String> p=str -> str.equals("a");
		List<String> list=Stream.of("a","b","c").filter(p).collect(Collectors.toList());
		
		Consumer<String> action=itme ->System.out.println(itme);
		list.stream().forEach(action);
	}
	
	@Test
	public void test5() {
		List<String> list1=Stream.of("a","b","c").collect(Collectors.toList());
		List<String> list2=Stream.of("d","e","f").collect(Collectors.toList());		
		Function<List<String>, Stream<String>> mapper = numbers -> numbers.stream();		
		List<String> together = Stream.of(list1,list2).flatMap(mapper).collect(Collectors.toList());
		together.stream().forEach(itme ->System.out.println(itme));
	}
	
	@Test
	public void test6() {
		List<Integer> list1=Stream.of(100,90,95).collect(Collectors.toList());
		
		Integer resMax=list1.stream().max(Comparator.comparingInt(inte -> inte.intValue())).get();
		System.err.println(resMax);
		
		ToIntFunction<Integer> keyExtractor=inte -> inte.intValue();
		Comparator<Integer> comparator=Comparator.comparingInt(keyExtractor);
		Integer resMin=list1.stream().min(comparator).get();
		System.err.println(resMin);
		
		
	}
	
	@Test
	public void test7() {
		BinaryOperator<Integer> accumulator=(acc, element) -> acc + element;
		int count=Stream. of(1, 2, 3).reduce(0, accumulator);
		System.out.println(count);
				
	}
	
	@Test
	public void test8() {
		Function<Integer, String> mapper = inte -> inte +"";
		List<String> list=Stream.of(1, 2, 3).filter(inte -> inte>=2).map(mapper).collect(Collectors.toList());
		list.forEach(itme -> System.out.println(itme));
	}
	
	
	
}
